home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS Toolkit
/
BBS Toolkit.iso
/
doors_1
/
gbs2bbs.zip
/
ANS2OANS.C
next >
Wrap
C/C++ Source or Header
|
1993-01-04
|
16KB
|
397 lines
/* Version 1.2 modified on 1-Nov-1987 by George A. Stanislav */
/* Version 1.1 modified on 21-Oct-1987 by George A. Stanislav */
/* */
/* Original by Wynn Wagner III */
#ifndef LINT_ARGS
#define LINT_ARGS
#endif
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <ctype.h>
#include "legible.h"
/*--------------------------------------------------------------------------*/
/* External declarations */
/*--------------------------------------------------------------------------*/
extern int errno;
/*--------------------------------------------------------------------------*/
/* Global declarations */
/*--------------------------------------------------------------------------*/
FILE *source = NULL;
FILE *dest = NULL;
int ansi_digit[4];
/*--------------------------------------------------------------------------*/
/* USAGE */
/*--------------------------------------------------------------------------*/
void usage()
begin
cputs("Converts GBS files into BBS files using Opus optimized video controls.\r\n\n\USAGE: Gbs2Bbs <filename>\r\n\n");
exit(1);
end /* usage */
/*--------------------------------------------------------------------------*/
/* CAN'T (do something) */
/*--------------------------------------------------------------------------*/
void cant( do_what, to_whom )
char *do_what;
char *to_whom;
begin
char s[100];
sprintf(s, "Can't %s %s", do_what, to_whom );
perror(s);
if (dest) fclose(dest);
if (source) fclose(source);
exit(1);
end /* Can't */
/*--------------------------------------------------------------------------*/
/* MAKE FILE NAME */
/* Build a new file name (node), but use a different tag. */
/*--------------------------------------------------------------------------*/
void make_file_name( node, tag )
char *node;
char *tag;
begin
register char *sptr;
if ( !(sptr=strchr(node,'.')) ) strcat(node,tag);
else strcpy(sptr,tag);
end /* make file name */
/*--------------------------------------------------------------------------*/
/* COMMAND */
/*--------------------------------------------------------------------------*/
void command(which)
byte which;
begin
int i;
if (ansi_digit[0]>0)
begin
for(i=0; i<ansi_digit[0]; i++)
begin
putc( 0x16, dest );
putc( which,dest );
end
return;
end
putc(0x16, dest);
putc(which,dest);
end /* command */
/*--------------------------------------------------------------------------*/
/* COLOR */
/*--------------------------------------------------------------------------*/
#define BACKGROUND(x) attribute=(attribute&0x8f)+(x)
#define FOREGROUND(x) attribute=(attribute&0xf8)+(x)
void color()
begin
register int i;
register int j;
int blink = 0;
static byte attribute = 0x07;
for(i=0; i<4; i++)
switch(ansi_digit[i])
begin
case 0: attribute = 0x07; break;
case 1: if (!(attribute & 0x0f))
attribute |= 0x0f;
else
attribute |= 0x08; break;
case 4: FOREGROUND(0x01); break;
case 5: blink = 0xffff; break;
case 7: attribute = 0x70; break;
case 30: FOREGROUND(0x00); break;
case 31: FOREGROUND(0x04); break;
case 32: FOREGROUND(0x02); break;
case 33: FOREGROUND(0x06); break;
case 34: FOREGROUND(0x01); break;
case 35: FOREGROUND(0x05); break;
case 36: FOREGROUND(0x03); break;
case 37: FOREGROUND(0x07); break;
case 40: BACKGROUND(0x00); break;
case 41: BACKGROUND(0x40); break;
case 42: BACKGROUND(0x20); break;
case 43: BACKGROUND(0x60); break;
case 44: BACKGROUND(0x10); break;
case 45: BACKGROUND(0x50); break;
case 46: BACKGROUND(0x30); break;
case 47: BACKGROUND(0x70); break;
end
if ((!blink) || attribute)
begin
putc( 0x16, dest );
putc( 0x01, dest );
putc( 0x10, dest );
attribute |= 0x80 ;
putc( attribute, dest );
end
if (blink)
begin
ansi_digit[0] = 0xffff;
command(0x02);
end
end
/*--------------------------------------------------------------------------*/
/* MAIN */
/*--------------------------------------------------------------------------*/
main(argc,argv)
int argc;
char *argv[];
begin
register char *sptr;
register int i;
char old_name[80];
char new_name[80];
char buffer[1024];
int line;
int j;
int handle;
/*--------------------------------------------------------------------*/
/* Say Howdy */
/*--------------------------------------------------------------------*/
cputs("Gbs2Bbs [1.2]\r\n");
if (argc<2) usage();
/*--------------------------------------------------------------------*/
/* Turn the existing file into a BAK */
/*--------------------------------------------------------------------*/
strcpy( new_name, argv[1] );
if (!strchr(new_name,'.')) make_file_name( new_name, ".Bbs" );
strcpy( old_name, new_name );
make_file_name( old_name, ".Bak" );
unlink( old_name );
if (rename(new_name,old_name)) cant("rename",old_name);
/*--------------------------------------------------------------------*/
/* Setup for the conversion */
/*--------------------------------------------------------------------*/
line =
errno = 0;
make_file_name( new_name, ".Bbs" );
source = fopen( old_name, "rb" );
if (errno) cant("open",old_name);
dest = fopen( new_name, "wb" );
if (errno) cant("create",new_name);
/*--------------------------------------------------------------------*/
/* The conversion */
/*--------------------------------------------------------------------*/
while(!feof(source))
begin
sptr = buffer;
*sptr = '\0';
fgets(sptr,1024,source);
line++;
while(1)
begin
switch( *sptr )
begin
default: /*--------------------------------------*/
/* Check on replicating characters */
/* (Needs to go down to .ASM badly!) */
/*--------------------------------------*/
for(i=1; sptr[i]==sptr[0]; i++)
;
if (i>=5)
begin
if (i==26)
begin
putc( *sptr,dest );
i--;
end
putc( 0x19, dest ); /* ctrl-Y */
putc( *sptr,dest ); /* value */
putc( i, dest ); /* factor */
sptr += i;
end
else
begin
putc(*sptr,dest);
sptr++;
end
break;
case 0x1a: /*--------------------------------------*/
/* End of the file */
/*--------------------------------------*/
goto done;
case 0: /*--------------------------------------*/
/* End of this line */
/*--------------------------------------*/
goto nxt;
case 0x1b: /*--------------------------------------*/
/* Escape (ANSI) */
/*--------------------------------------*/
for(i=0; i<4; i++)
ansi_digit[i] = 0xffff;
i = 0;
sptr++;
if (*sptr!='[') break;
sptr++;
for(;;) switch (*sptr)
begin
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
if (i>4)
begin
cprintf("Too complicated, line %d",line);
exit(1);
end
ansi_digit[i] = atoi(sptr);
do sptr++; while(isdigit(*sptr));
i++;
break;
case ';' : sptr++;
break;
case 'k' : /* erase EOL */
command(0x07);
sptr++;
goto ansi_done;
case 'A' : /* go up */
command(0x03);
sptr++;
goto ansi_done;
case 'B' : /* go down */
command(0x04);
sptr++;
goto ansi_done;
case 'C' : /* go right */
command(0x06);
sptr++;
goto ansi_done;
case 'D' : /* go left */
command(0x05);
sptr++;
goto ansi_done;
case 'J' : /* clear EOS */
putc( 0x0c, dest );
sptr++;
goto ansi_done;
case 'm' : /* color */
color();
sptr++;
goto ansi_done;
case 'f' : /* set cursor position */
case 'H' : /* set cursor position */
if (ansi_digit[0]<0)
ansi_digit[0] = 0;
if (ansi_digit[1]<0)
ansi_digit[1] = 0;
putc(0x16, dest);
putc(0x08, dest);
putc(ansi_digit[0],dest);
putc(ansi_digit[1],dest);
sptr++;
goto ansi_done;
case 's' : /* save cursor position */
sptr++;
sptr[0] = '\0';
goto ansi_done;
case 'u' : /* restore cursor position */
sptr++;
goto ansi_done;
default : /* we can't do this */
cprintf("Unknown ANSI command, line %d\n",line);
exit(1);
end /* switch */
break;
end /* switch */
ansi_done: ;
end
nxt: ;
end
done:
fclose(source);
fclose(dest);
end /* main */
/* END OF FILE: make file name */